package com.hmall.item.listener;

import cn.hutool.json.JSONUtil;
import com.hmall.api.client.PayClient;
import com.hmall.api.domain.dto.ItemDTO;
import com.hmall.api.domain.dto.PayOrderDTO;
import com.hmall.api.domain.po.Item;
import com.hmall.api.domain.po.ItemDoc;
import com.hmall.api.domain.po.Order;

import com.hmall.common.utils.BeanUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@RequiredArgsConstructor
@Slf4j
public class ItemListener {
    private  RestHighLevelClient client;
    @BeforeEach
    void setUp() {
        client = new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://192.168.150.101:9200")
        ));
    }

    @AfterEach
    void tearDown() throws IOException {
        client.close();
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "Insert.queue",durable = "true"),
            exchange =@Exchange(name = "Insert.direct"),
            key={"Insert"}
    ))
    public void InsertListening(ItemDTO item) throws IOException {
        ItemDoc itemDoc = BeanUtils.copyProperties(item, ItemDoc.class);
        String doc= JSONUtil.toJsonStr(itemDoc);
        IndexRequest request=new IndexRequest("items").id(itemDoc.getId());
        request.source(doc, XContentType.JSON);
        client.index(request, RequestOptions.DEFAULT);
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "Update.queue",durable = "true"),
            exchange =@Exchange(name = "Update.direct"),
            key={"Update"}
    ))
    public void UpdateListening(ItemDTO item) throws IOException {
        ItemDoc itemDoc = BeanUtils.copyProperties(item, ItemDoc.class);
        String doc= JSONUtil.toJsonStr(itemDoc);
        IndexRequest request=new IndexRequest("items").id(itemDoc.getId());
        request.source(doc, XContentType.JSON);
        client.index(request, RequestOptions.DEFAULT);
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "Delete.queue",durable = "true"),
            exchange =@Exchange(name = "Delete.direct"),
            key={"Delete"}
    ))
    public void DeleteListening(Long id) throws IOException {
        DeleteRequest request=new DeleteRequest("items").id(id+"");
        client.delete(request, RequestOptions.DEFAULT);
    }
}
